package org.example.managere.controller.feibiao;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmAapModelInfoDto;
import org.example.managere.service.feibiao.AmAapModelInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 模型融合配置表Controller
 */
@RestController
@RequestMapping("/api/feibiao/aap/modelinfo")
@RequiredArgsConstructor
public class AmAapModelInfoController {

    private final AmAapModelInfoService service;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public ResponseEntity<Page<AmAapModelInfoDto>> list(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "id,desc") String sort) {
        String[] sortParams = sort.split(",");
        Sort.Direction direction = sortParams.length > 1 && "desc".equalsIgnoreCase(sortParams[1]) ?
                Sort.Direction.DESC : Sort.Direction.ASC;
        PageRequest pageRequest = PageRequest.of(page, size, Sort.by(direction, sortParams[0]));
        return ResponseEntity.ok(service.findAll(pageRequest));
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public ResponseEntity<AmAapModelInfoDto> getById(@PathVariable Integer id) {
        return ResponseEntity.ok(service.findById(id));
    }

    /**
     * 新增
     */
    @PostMapping
    public ResponseEntity<AmAapModelInfoDto> create(@Valid @RequestBody AmAapModelInfoDto dto) {
        return ResponseEntity.ok(service.save(dto));
    }

    /**
     * 更新
     */
    @PutMapping("/{id}")
    public ResponseEntity<AmAapModelInfoDto> update(
            @PathVariable Integer id,
            @Valid @RequestBody AmAapModelInfoDto dto) {
        return ResponseEntity.ok(service.update(id, dto));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        service.delete(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 搜索
     */
    @GetMapping("/search")
    public ResponseEntity<Page<AmAapModelInfoDto>> search(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return ResponseEntity.ok(service.search(keyword, pageRequest));
    }
} 